<%--
  Created by IntelliJ IDEA.
  User: 11838
  Date: 2020/11/11
  Time: 17:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>设施类型</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/style/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/style/layui/layui.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/style/jquery-2.1.0.js"></script>
</head>
<body>
    <%--查询--%>
    <div id="min">
        <form class="layui-form" action="" id="adataform" lay-filter="ggg">

            <div class="layui-inline">
                <label class="layui-form-label">设施名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="sname" name="sname" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">设施类型</label>
                <div class="layui-input-inline">
                    <input type="text" id="stypeid" name="stypeid" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-input-inline">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo2">查询</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="re">重置</button>
            </div>

        </form>
    </div>

    <div id="caozuo" style="display: none">
        <input type="hidden" id="hid">
        <button id="one" class="layui-btn layui-btn-warm layui-btn-radius">下架</button>
        <button id="two" class="layui-btn layui-btn-danger layui-btn-radius" >恢复</button>
    </div>


<button type="button" class="layui-btn" id="addbtn">
    <i class="layui-icon-sm">&#xe608;</i> 添加
</button>
<script>


</script>
<table class="layui-hide" id="test" lay-filter="test"></table>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <button lay-event="caozuo" class="layui-btn layui-btn-warm layui-btn-xs">操作</button>
    <%--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>--%>
</script>

<script>

    layui.use(['table','form'], function(){
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test' //id为test的table
            ,url:'${pageContext.request.contextPath}/facility/selectFacility'   //url 查询数据   所有的客户的数据
            // ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]

            ,title: '设施表'
            //data[{ id:12,name:123}]

            ,cols: [[
                {field:'id', title:'ID',fixed: 'left',  sort: true}
                ,{field:'name', title:'设施名称'}
                ,{field:'count', title:'数量',sort: true}
                ,{field:'typeid', title:'类型id',  sort: true}
                ,{field:'pmoney', title:'定价（房间里的设施损坏需要赔偿）', width:300, sort: true}
                ,{field:'gys', title:'供应商',}
                ,{field:'status', title:'状态',templet:function (data){
                        if (data.status==0){
                            return "正常";
                        }else {
                            return "已下架";
                        }
                    }}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
            , done: function (res, curr, count) {
                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    if (item.status === 1) {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                        tr.css("background-color", "gray");//目标单元格颜色
                        tr.css("color", "white");//被选中的单元格字体颜色 ：后勤部
                    }
                });
            }, parseData: function (res) {
                if (res.code == -1) {
                    layuiHelp.AlertMsg(res.msg, res.icon);
                }
            }

        });

        form.on('submit(demo2)',function (data){

            table.reload("test",{
                page:{
                    curr:1
                },where:data.field
            });
            return false;
        });

        $("#re").click(function (){
           location=location
        });

        //监听行工具事件  test  表格的id
        table.on('tool(test)', function(obj){
            //obj 代表 一行的内容
            var data = obj.data; //一行的数据
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除了吗', function(index){
                    $.ajax({
                        url: "${pageContext.request.contextPath}/facility/DelFacility",
                        data:data,
                        success:function(r){
                            //弹出删除成功
                            layer.msg("删除成功！！！");
                            //刷新table
                            layer.close(index);
                            table.reload("test",{
                                page:{
                                    //从第一页开始加载
                                    curr:1
                                }
                            })
                        },error:function (r) {
                            alert(JSON.stringify(r))
                        }
                    });
                    layer.close(index);
                    return false;
                });
            } else if(obj.event === 'edit'){
                $("#typeid").empty();
                display();
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.open({
                        title:'修改信息'
                        ,type: 1
                        ,content:$('#add')
                        ,offset: '50px'
                        ,moveOut:true
                        ,area: ['500px', '450px']
                    });
                    form.val('upd', {
                        "id":data.id
                        ,"name":data.name
                        ,"count":data.count
                        ,"typeid":data.bid
                        ,"pmoney":data.pmoney
                        ,"gys":data.gys
                    });
                });
            }else if (obj.event === 'caozuo') {
                /*confirmTrans: function(){*/
                //配置一个透明的询问框
                layer.msg('', {
                    content: $("#caozuo"),
                    closeBtn: 1,
                    time: 0,
                    cancel: function () {
                        location = location;
                    },
                    success: function () {
                        $("#hid").val(data.id)
                    }
                });
            }


            $("#one").click(function (){
                var id=$("#hid").val();
                /* layer.closeAll();*/
                $.ajax({
                    url:"${pageContext.request.contextPath}/facility/bad",
                    data:{id:id},
                    async:false,
                    success:function (d){
                        if (d>0){
                            layer.msg("已停用")
                        }
                    },error:function (){
                        alert(55)
                    }
                })
                setTimeout(function() {
                    window.location.reload();
                },400);
            })

            $("#two").click(function (){
                var id=$("#hid").val();
                /* layer.closeAll();*/
                $.ajax({
                    url:"${pageContext.request.contextPath}/facility/good",
                    data:{id:id},
                    async:false,
                    success:function (d){
                        if (d>0){
                            layer.msg("已恢复");
                        }
                    },error:function (){
                        alert(66)
                    }
                })
                setTimeout(function() {
                    window.location.reload();
                },400);
            })

        });



        $("#addbtn").click(function () {
            $('#addform')[0].reset();
            display();
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.open({
                    title:'修改信息'
                    ,type: 1
                    ,content: $("#add")
                    ,offset: '50px'
                    ,moveOut:true
                    ,area: ['560px', '450px']
                });
            })
        });
        form.on('submit(demo11)',function (data) {
            $.ajax({
                url:"${pageContext.request.contextPath}/facility/addAndUpdFAC",
                data:data.field,
                success:function(){
                    layer.closeAll();
                    table.reload("test");
                    $('#addform')[0].reset();
                    form.render();
                },
                error:function () {
                    layer.closeAll();
                    alert("操作失败");
                }
            });
            return false;
        });
        function display(){
            //房间号码下拉框填充
            $.ajax({
                url:'${pageContext.request.contextPath}/facility/selectTypeid'
                ,data:{}
                ,type:'post'
                ,async:false
                ,dateType:'json'
                ,success:function (data) {
                    var option = '';
                    for(var i = 0 ; i < data.length; i++){
                        option += '<option value="'+data[i].id+'">'+data[i].name+'</option>'
                    }
                    $("#typeid").append(option);
                    form.render("select");
                }
                ,error:function(data) {
                    alert("错误");
                }
            });
        }
    });


</script>
<div class="layui-row" id="add" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" lay-filter="upd" method="post" style="margin-top:20px" id="addform">
            <div class="layui-form-item">
                <input type="hidden"  id="id" name="id"  required autocomplete="off"  class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">设备名称</label>
                <div class="layui-input-block">
                    <input type="text"  id="name" name="name"  required  lay-verify="required" autocomplete="off"  class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">数量</label>
                <div class="layui-input-block">
                    <input type="text" id="count" name="count"  required  lay-verify="required" autocomplete="off"  class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">设施类型</label>
                <div class="layui-input-block">
                    <select id="typeid" name="typeid">
                    </select>
<%--                    <input type="text"id="typeid" name="typeid"  required  lay-verify="required" autocomplete="off"  class="layui-input">--%>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">定价</label>
                <div class="layui-input-block">
                    <input type="text"id="pmoney" name="pmoney"  required  lay-verify="required" autocomplete="off"  class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-block">
                    <input type="text"id="gys" name="gys"  required  lay-verify="required" autocomplete="off"  class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="demo11">确认修改</button>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
</html>
